package com.it.xyx.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.xyx.demo.entity.Account;
import com.it.xyx.demo.service.IAccountService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.List;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyx
 * @since 2024-08-22
 */
@RestController
@RequestMapping
public class AccountController {
    @Resource
    private IAccountService accountService;

    @PostMapping(value = "/storage/decrease")
    String decrease(@RequestParam("productId") Long productId
            , @RequestParam("money") Long money) {

        Account account = accountService.getById(productId);
        BigDecimal total = account.getTotal();
        BigDecimal used = account.getUsed();
        BigDecimal residue = account.getResidue();
        BigDecimal num = new BigDecimal(money);
        total = total.subtract(num);
        used = used.add(num);
        residue = residue.subtract(num);
        account.setUsed(used);
        account.setResidue(residue);
        account.setTotal(total);
        accountService.updateById(account);

        //测试异常
        if (productId != null) {
            throw new RuntimeException();
        }

        return "success";
    }


}

